import { LoadingUtil } from './../../common/loading';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';
import { BusiserviceProvider } from '../../providers/busiservice/busiservice';
import { UiserviceProvider } from '../../providers/uiservice/uiservice';

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  username: string;
  password: string;
  type: number;
  loading:LoadingUtil;
  constructor(public navCtrl: NavController, public navParams: NavParams, public busi: BusiserviceProvider, private uiservice: UiserviceProvider,private loadingCtrl: LoadingController) {
    this.type = this.navParams.get("type");
    this.loading = new LoadingUtil(this.loadingCtrl);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  login() {
    if (this.username == null) {
      alert("用户名为空!");
      return;
    }
    if (this.password == null) {
      alert("密码为空!");
      return;
    }
    this.loading.showLoading();
    this.busi.login(this.username, this.password).subscribe(
      data => {
        this.loading.hideLoading();
        if (data._success) {
          //用户id
          localStorage.setItem("userid", data.result.userid);
          this.navCtrl.push("TabsPage");
        } else {
          this.uiservice.showAlert(data._message);
        }
      },
      error => {
        this.loading.hideLoading();
        this.uiservice.showAlert("网络异常!");
      }
    );
  }
  gotosetting(){
    this.navCtrl.push("ServerSettingPage");
  }
  regi(){
    this.navCtrl.push("RegiPage")
  }
}
